/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.pima.gestrest.controller;

import com.pima.gestrest.business.RestaurantEJB;
import com.pima.gestrest.model.Restaurant;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;

/**
 *
 * @author Pierre-Louis Conte
 */

@ManagedBean
@SessionScoped
public class RestaurantController {
    
    @EJB
    private RestaurantEJB restaurantEJB;
    private HtmlDataTable dataTable;
    private Restaurant restaurant = new Restaurant();
    private ListDataModel restaurantList;
    private ArrayList<SelectItem> itemList;

    private void updateItemList() {
        itemList = new ArrayList<SelectItem>(restaurantEJB.getItems());
    }

    private void updateRestaurantList() {
        restaurantList = new ListDataModel(restaurantEJB.findAll());
    }

    // ======================================
    // =          Getters & Setters         =
    // ======================================

    public HtmlDataTable getDataTable() {
        return dataTable;
    }

    public void setDataTable(HtmlDataTable dataTable) {
        this.dataTable = dataTable;
    }

    public Restaurant getRestaurant() {
        return restaurant;
    }

    public void setRestaurant(Restaurant restaurant) {
        this.restaurant = restaurant;
    }

    public ListDataModel getRestaurantList() {
        updateRestaurantList();
        return restaurantList;
    }

    public void setRestaurantList(ListDataModel restaurantList) {
        this.restaurantList = restaurantList;
    }

    public List<SelectItem> getItemList() {
        updateItemList();
        return itemList;
    }

    public void setItemList(ArrayList<SelectItem> list) {
        itemList = list;
    }

    // ======================================
    // =           Public Methods           =
    // ======================================

    public String doNew() {
        restaurant = new Restaurant();
        return "../secured/newRestaurant.faces";
    }

    public String doCreate() {
        restaurantEJB.create(restaurant);
        return "../resources/listRestaurants.faces";
    }

    public String doDelete() {
        List<Restaurant> restaurants = (List<Restaurant>)restaurantList.getWrappedData();
        restaurantEJB.delete(onlySelected(restaurants));
        return "../resources/listRestaurants.faces";
    }

    private List<Restaurant> onlySelected(List<Restaurant> list) {
        for (Iterator<Restaurant> it = list.iterator(); it.hasNext(); ) {
            if (!(it.next().isSelected()))
                it.remove();
        }
        return list;
    }

    public String viewDetail () {
        restaurant = (Restaurant)restaurantList.getRowData();
        return "../resources/viewRestaurant.faces";
    }

    public String doEdit() {
        //restaurant = (Restaurant)restaurantList.getRowData(); // Voici comment on trouve le restaurant selected
        return "../secured/editRestaurant.faces";
    }

    public String doSave() {
        restaurantEJB.update(restaurant);
        return "../resources/listRestaurants.faces";
    }


}
